package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.TeacherBo;
import com.ruoyi.system.domain.vo.TeacherVo;
import com.ruoyi.system.domain.Teacher;
import com.ruoyi.system.mapper.TeacherMapper;
import com.ruoyi.system.service.ITeacherService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 教师信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-30
 */
@RequiredArgsConstructor
@Service
public class TeacherServiceImpl implements ITeacherService {

    private final TeacherMapper baseMapper;

    /**
     * 查询教师信息
     */
    @Override
    public TeacherVo queryById(Long teacherId){
        return baseMapper.selectVoById(teacherId);
    }

    /**
     * 查询教师信息列表
     */
    @Override
    public TableDataInfo<TeacherVo> queryPageList(TeacherBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Teacher> lqw = buildQueryWrapper(bo);
        Page<TeacherVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询教师信息列表
     */
    @Override
    public List<TeacherVo> queryList(TeacherBo bo) {
        LambdaQueryWrapper<Teacher> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Teacher> buildQueryWrapper(TeacherBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Teacher> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTeacherNo()), Teacher::getTeacherNo, bo.getTeacherNo());
        lqw.like(StringUtils.isNotBlank(bo.getTeacherName()), Teacher::getTeacherName, bo.getTeacherName());
        lqw.eq(StringUtils.isNotBlank(bo.getGender()), Teacher::getGender, bo.getGender());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), Teacher::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), Teacher::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), Teacher::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getDepartment()), Teacher::getDepartment, bo.getDepartment());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Teacher::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增教师信息
     */
    @Override
    public Boolean insertByBo(TeacherBo bo) {
        Teacher add = BeanUtil.toBean(bo, Teacher.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTeacherId(add.getTeacherId());
        }
        return flag;
    }

    /**
     * 修改教师信息
     */
    @Override
    public Boolean updateByBo(TeacherBo bo) {
        Teacher update = BeanUtil.toBean(bo, Teacher.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Teacher entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除教师信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
